package cn.cdeden.knowledge.service.impl;

import cn.cdeden.common.core.utils.MapstructUtils;
import cn.cdeden.common.core.utils.page.TableDataInfo;
import cn.cdeden.common.mybatis.validator.QueryHelpMybatisPlus;
import cn.cdeden.knowledge.domain.dto.SysFormDocDto;
import cn.cdeden.knowledge.domain.dto.SysFormDocQueryParam;
import cn.cdeden.knowledge.service.SysFormDocFileService;
import cn.cdeden.knowledge.utils.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.cdeden.knowledge.domain.SysFormDoc;
import cn.cdeden.knowledge.mapper.SysFormDocMapper;
import cn.cdeden.knowledge.service.SysFormDocService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = SysTextDataService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class SysFormDocServiceImpl extends ServiceImpl<SysFormDocMapper, SysFormDoc> implements SysFormDocService{


    private final SysFormDocMapper sysFormDocMapper;

    private final SysFormDocFileService sysFormDocFileService;

    @Override
    public TableDataInfo<SysFormDocDto> queryAll(SysFormDocQueryParam query, Pageable pageable) {
        IPage<SysFormDoc> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<SysFormDoc> page = sysFormDocMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));

        return MapstructUtils.convertTableDataInfo(TableDataInfo.build(page), SysFormDocDto.class);
    }

    @Override
    public List<SysFormDocDto> queryAll(SysFormDocQueryParam query){
        return MapstructUtils.convert(sysFormDocMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), SysFormDocDto.class);
    }

    @Override
    public SysFormDocDto query(SysFormDocQueryParam query) {
        QueryWrapper<SysFormDoc> predicate = QueryHelpMybatisPlus.getPredicate(query);
        return MapstructUtils.convert(sysFormDocMapper.selectOne(predicate), SysFormDocDto.class);
    }

    @Override
    public SysFormDoc getById(Integer id) {
        return sysFormDocMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public SysFormDocDto findById(Integer id) {
        return MapstructUtils.convert(getById(id), SysFormDocDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysFormDoc insert(SysFormDocDto resources) {
        SysFormDoc entity = MapstructUtils.convert(resources, SysFormDoc.class);
        this.save(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(SysFormDocDto resources){
        SysFormDoc entity = MapstructUtils.convert(resources, SysFormDoc.class);
        boolean ret = this.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByIds(Set<Integer> ids){
        // delCaches(ids);
        return this.removeByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.deleteByIds(set);
    }
    @Override
    public boolean deleteByTreeNodeId(String treeNodeId, String type) {
        QueryWrapper<SysFormDoc> predicate = QueryHelpMybatisPlus.getPredicate(new SysFormDocQueryParam());
        predicate.eq("tree_node_id",treeNodeId);
        predicate.eq("type",type);
        SysFormDoc sysFormDoc = sysFormDocMapper.selectOne(predicate);
        SysFormDocDto sysFormDocDto = MapstructUtils.convert(sysFormDoc, SysFormDocDto.class);
//        SysFormDocDto sysFormDocDto = ConvertUtil.convert(sysFormDoc, SysFormDocDto.class);
        Integer id = sysFormDocDto.getId();
        //删除附件
        sysFormDocFileService.deleteByFormDocId(id);
        return this.removeById(id);
    }
}
